//
//  MSMainViewController.m
//  HelloMusic
//
//  Created by xuehan on 15/3/19.
//  Copyright (c) 2015年 gpr. All rights reserved.
//

#import "MSMainViewController.h"
#import "MSMusicTool.h"
#import "MSArtist.h"
#import "MSMusicSearchResponse.h"
#import "MSSong.h"
//#import "MSSearchModel.h"

@interface MSMainViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic,strong) MSMusicSearchResponse *response;
//@property (nonatomic,strong) MSSearchModel *searchModel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;



@end


static NSString *ID = @"Main_cell";
@implementation MSMainViewController


#pragma mark - 初始化
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self loadData:@"l"];

}
- (void)loadData:(NSString *)key{
    
    [MSMusicTool searchWithKeyWord:key complete:^(MSMusicSearchResponse *response, NSError *error) {
        if (response!= nil){
            self.response = response;
            [self.tableView reloadData];
        }
    }];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
}

#pragma mark -懒加载
- (MSMusicSearchResponse *)response{
    
    if(_response == nil){
        _response = [[MSMusicSearchResponse alloc]init];
    }
    return _response;
    
}

//- (MSSearchModel *)searchModel
//{
//    if(_searchModel == nil){
//        _searchModel = [[MSSearchModel alloc]init];
//    }
//    return _searchModel;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.response.song.count + self.response.artist.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    if (indexPath.row < self.response.artist.count) {
        cell.textLabel.text = [self.response.artist[indexPath.row] artistname];
    }else{
    
        cell.textLabel.text =[NSString stringWithFormat:@"%@ - %@",[self.response.song[indexPath.row-self.response.artist.count] artistname],[self.response.song[indexPath.row-self.response.artist.count] songname]] ;

    }
    return cell;

}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section{
    
    UIView *footerView = [[UIView alloc]init];
    
    footerView.backgroundColor = [UIColor greenColor];
    
    return footerView;
    
}

#pragma mark -搜索条代理方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;{
    // 取消响应
    [self.searchBar resignFirstResponder];
    
    [self loadData:searchBar.text];
    
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
   [self loadData:searchBar.text];
    
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    
    [self.searchBar becomeFirstResponder];
    
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
//    MSLog(@"quxiao");
}



@end
